<template>
  <section>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="部门名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="120">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex">
      </el-table-column>
      <el-table-column prop="manager_id" label="负责人" width="120">
      </el-table-column>
      <el-table-column prop="parent_id" label="上级部门" min-width="180">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes,prev, pager, next"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-sizes="[5, 10, 20, 50]"
                     :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="editForm">
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门主管">
          <el-input v-model="addForm.manager_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级部门">
          <el-input v-model="addForm.parent_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>


<script>
import {addUser, batchRemoveUser, removeUser} from "../../api/api";
import util from "../../common/js/util";

export default {
  data() {
    return {
      //  列表展示的数据
      listData:[],
      //  列表加载效果
      listLoading: true,
      //  列表选中列
      sels: [],
      //  总条数默认为0
      total:0,
      //  每页默认展示的数据的条数
      pageSize:10,
      //展示的第几页
      page:1,
      filters: {
        name: ''
      },
      //新增界面是否显示
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ]
      },
      //新增界面数据
      addForm: {
        sn:'',
        name:"",
        state:0,
        manager_id:null,
        parent_id:null
      },
      //  新增或修改列表的标题
      title:'',
    }
  },
  methods: {
    //新增
    addSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //  开启提交的加载效果
            this.addLoading = true;
            //  发送请求，提交数据
            this.$http.put("/department",this.addForm).then(res=>{
              //  关闭提交的加载效果
              this.addLoading = false;
              //  解构表达式
              let {success,message} = res.data;
              if (success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                //  后台逻辑判断失败  提示信息
                this.$message.error('网络异常');
              }
            })
            //  关闭模态框
            this.addFormVisible = false;
            //  刷新列表
            this.loadListData();
          });
        }
      });
    },
    //显示新增界面
    handleAdd() {
      //  清空表单方法一
      this.addForm = {
        sn: '',
        name: "",
        state: 0,
        manager_id: null,
        parent_id: null
      };
      //  清空表单方法二
      //this.addForm = Object.assign({}, {});
      //  弹出模态框(添加表单视图)
      this.addFormVisible = true;
      this.title='新增';

    },
    //批量删除
    batchRemove() {
      //var ids = this.sels.map(item => item.id).toString();
      var ids = this.sels.map(item => item.id);

      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //  开启加载效果
        this.listLoading = true;
        //  发送批量删除请求
        this.$http.patch("/department",ids).then(res=>{
          //  解构表达式
          let {success,message} = res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            //  后台逻辑判断失败  提示信息
            this.$message.error('网络异常');
          }
        })
        //  刷新列表
        this.loadListData();
      }).catch(() => {
        //  取消删除
        this.$message('取消删除');
      });
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.loadListData();
    },
    handleCurrentChange(val) {
      //val是要展示的页码的数据
      this.page = val;
      this.loadListData();
    },
    //  提供一个发送请求的方法
    loadListData(){
      let param = {
        //  当前页
        currentPage:this.page,
        //  每页展示条数
        pageSize:this.pageSize,
        keywords:this.filters.name
      };
      //  发送axios请求,请求路径为后端接口
      this.$http.post("/department",param).then(res=>{
        console.log(res);
        this.listData = res.data.data;
        //  后端数据总条数
        this.total = res.data.totals;
        //  数据效应之后，取消列表加载效果
        this.listLoading = false;
      })
    },
    selsChange(sels) {
      this.sels = sels;
    },
    //性别显示转换
    formatSex(row, column) {
      return row.state == 0 ? '启用' : row.state == -1 ? '禁用' : '未知';
    },
    //显示编辑界面
    handleEdit(index, row) {
      //  回显数据
      this.addForm = Object.assign({}, row);
      //  弹出模态框
      this.addFormVisible = true;
      //  将模态框的标题设置为修改
      this.title = '修改';
    },
    //删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //  开启加载效果
        this.listLoading = true;
        console.debug(row.id);
        //  发送delete请求
        this.$http.delete("/department/"+row.id).then(res=>{
          console.debug(res);
          //  解构表达式
          let {success,message} = res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            //  后台逻辑判断失败  提示信息
            this.$message.error('网络异常');
          }
        })
        //  刷新列表
        this.loadListData();
      }).catch(() => {
        //  取消删除
        this.$message('取消删除');
      });
    },

  },
  //  钩子函数
  mounted() {
    //  页面一加载的时候就调用加载数据的方法
    this.loadListData();
  }

}
</script>

<style scoped>

</style>